package com.podcastellano.radiopodapp;
import java.io.IOException;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;

public class Reproductor extends Activity {
	
	
	private static final String LOG_TAG = "AudioRecordTest";
    private static String mFileName = null;//Cadena con la ruta a la fuente de audio
    private MediaPlayer   mPlayer = null;//Declaracion del objeto mediaplayer
    private AudioManager audioManager;
    private Intent serviceIntent;
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	
    	
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
        int maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
        int curVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
        SeekBar volControl = (SeekBar)findViewById(R.id.SeekBar01);
        volControl.setMax(maxVolume);
        volControl.setProgress(curVolume);
        volControl.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

  
    public void onStopTrackingTouch(SeekBar arg0) {
     

    }

  
    public void onStartTrackingTouch(SeekBar arg0) {
   

    }

   
    public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
    
     audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, arg1, 0);
    }
   });
        final Button btnRep = (Button)findViewById(R.id.btnRep);
        final Button btnStop = (Button)findViewById(R.id.btnStop);
        final TextView txvAlbum = (TextView)findViewById(R.id.txvAlbum);
        final TextView txvPodcast = (TextView)findViewById(R.id.txvPodcast);
        final ImageView imgRssImg= (ImageView)findViewById(R.id.imagen);
        
        serviceIntent = new Intent(this, PodService.class);
        
        mFileName = "http://radiopodcast.serverroom.us:5742";
		startService(serviceIntent);
		RssParser prsPlaying= new RssParser("http://radio.podcastellano.org/now_playing.xml");
        Listening playingNow=(Listening) prsPlaying.parse();
        imgRssImg.setImageBitmap(playingNow.getImagen());
        imgRssImg.setScaleType(ImageView.ScaleType.FIT_XY);
        txvAlbum.setText(playingNow.getTitulo());
        txvPodcast.setText(playingNow.getAlbum());
        
        btnRep.setOnClickListener(new View.OnClickListener() {
            
            public void onClick(View arg0)
            {
            	
            		
            		mFileName = "http://62.212.74.140:8206";
            		startService(serviceIntent);
            		RssParser prsPlaying= new RssParser("http://radio.podcastellano.org/now_playing.xml");
                    Listening playingNow=(Listening) prsPlaying.parse();
                    imgRssImg.setImageBitmap(playingNow.getImagen());
                    imgRssImg.setScaleType(ImageView.ScaleType.FIT_XY);
                    txvAlbum.setText(playingNow.getTitulo());
                    txvPodcast.setText(playingNow.getAlbum());
                   /* if(img!=null){
                    	img.setBounds(0,0, 120, 120);
                    	imgRssImg.setImageDrawable(img);
                    	
                    	imgRssImg.setScaleType(ImageView.ScaleType.CENTER);
                    	
                    }*/
            		
            	
            }
        });
        
btnStop.setOnClickListener(new View.OnClickListener() {
            
            public void onClick(View arg0)
            {
            	
            	//stopPlaying();
            	stopService(serviceIntent);
            	
            }
        });
        
    }
   
    
    
}